package com.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javabean.Product;
import com.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author: 张波
 * @Time: 2025/8/27 10:55
 */
@Controller
@RequestMapping("/product")
public class ProductController {
	@Autowired
	public ProductController(ProductService productService) {
		this.productService = productService;
	}
	ProductService productService;
	/**
	 * 查询所有产品
	 */
	@RequestMapping("/findAll")
	public String findAll(HttpServletRequest request, @RequestParam(name = "page",defaultValue = "1")Integer page, @RequestParam(name = "limit",defaultValue = "5") Integer limit, @RequestParam(value ="productName",defaultValue = "") String productName){
		PageHelper.startPage(page,limit);
		List<Product> all = productService.findProductName(productName);
		PageInfo<Product> pageInfo = new PageInfo<>(all);
		request.setAttribute("pageInfo",pageInfo);
		request.setAttribute("value",productName);
		return "/product/product-list";
	}
	/// 根据id进行查询
	@RequestMapping("/findById")
	public String findById(HttpServletRequest request,int id){
		Product product = productService.findById(id);
		request.setAttribute("product",product);
		return "/product/product-edit";
	}
	/// 根据id进行修改
	@RequestMapping("/edit")
	public String edit(Product product){
		productService.updateProduct(product);
		return "redirect:findAll";
	}
	/// 添加一条数据
	@RequestMapping("/add")
	public String add(Product product){
		productService.addProduct(product);
		return "redirect:findAll";
	}
	/// 根据id删除一条数据
	@RequestMapping("/deleteById")
	public String delete(int id){
		productService.deleteById(id);
		return "redirect:findAll";
	}
	@RequestMapping(value = "/deleteIds",method = RequestMethod.POST)
	@ResponseBody
	public String deleteAll( @RequestBody List<Integer> ids){
		productService.deleteMore(ids);
		return "/product/findAll";
	}
}
